package com.usian.controller;

import com.usian.feign.ItemServiceFeign;
import com.usian.pojo.TbItem;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 韩耀东
 * @date 2021/1/5 20:48
 */
@RestController
@RequestMapping("/backend/item")
public class ItemController {
    @Autowired
    private ItemServiceFeign itemServiceFeign;

    @RequestMapping("/selectItemInfo")
    public Result selectItemInfo(Long id) {
        TbItem tbItem = itemServiceFeign.selectItemInfo(id);
        if (tbItem == null) {
            return Result.error("未查到，该记录");
        }
        return Result.ok(tbItem);
    }

    @RequestMapping("/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "5") Integer rows) {
        PageResult pageResult = itemServiceFeign.selectTbItemAllByPage(page, rows);
        if (pageResult == null || pageResult.getResult().size() == 0) {
            return Result.error("未查到，该记录");
        }
        return Result.ok(pageResult);
    }

    /**
     * @return com.usian.utils.Result
     * @Author 韩耀东
     * @Description //TODO 添加 商品
     * @Date 2021/1/6 11:04
     * @Param [tbItem, desc, itemParams]
     **/
    @RequestMapping("/insertTbItem")
    public Result insertTbItem(TbItem tbItem, String desc, String itemParams) {
        Integer insertTbItemNum = itemServiceFeign.insertTbItem(tbItem, desc, itemParams);
        if (insertTbItemNum == 3) {
            return Result.ok();
        }
        return Result.error("添加失败");

    }

    /**
     * @return com.usian.utils.Result
     * @Author 韩耀东
     * @Description //TODO 修改 商品
     * @Date 2021/1/6 11:04
     * @Param [tbItem, desc, itemParams]
     **/
    @RequestMapping("/updateTbItem")
    public Result updateTbItem(TbItem tbItem, String desc, String itemParams) {
        Integer insertTbItemNum = itemServiceFeign.updateTbItem(tbItem, desc, itemParams);
        if (insertTbItemNum == 3) {
            return Result.ok();
        }
        return Result.error("修改失败");

    }

    /**
     * @return java.util.Map<java.lang.String               ,               java.lang.Object>
     * @Author 韩耀东
     * @Description //TODO 修改数据的回显
     * @Date 2021/1/6 14:08
     * @Param [itemId]
     **/
    @RequestMapping("/preUpdateItem")
    public Result preUpdateItem(Long itemId) {
        Map<String, Object> map = itemServiceFeign.preUpdateItem(itemId);
        if (map == null) {
            return Result.error("未查到，该记录");
        }
        return Result.ok(map);
    }
    /**
     * @Author 韩耀东
     * @Description //TODO 删除商品
     * @Date 2021/1/6 18:40
     * @Param [itemId]
     * @return com.usian.utils.Result
     **/
    @RequestMapping("/deleteItemById")
    public Result deleteItemById(Long itemId) {
        Integer status = itemServiceFeign.deleteItemById(itemId);
        if (status == 0) {
            return Result.error("删除失败");
        }
        return Result.ok(status);
    }
}
